package com.diandiansong.manager.ui.biz;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.utils.Api;
import com.diandiansong.manager.entity.SalesUserOrder_listInfo;
import com.diandiansong.manager.utils.M;

import java.util.List;

import diandiansong.library.ui.base.BaseAct;
import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;
/**
 * 客户订单
 */
public class CustomerOrder extends BaseAct {
    static String mmember_id;
    static String mdateinfo;

    public static void start(Context context, String member_id) {
        mmember_id = member_id;
        mdateinfo = null;
        Intent starter = new Intent(context, CustomerOrder.class);
        context.startActivity(starter);
    }

    public static void start(Context context, String member_id, String dateinfo) {
        mmember_id = member_id;
        mdateinfo = dateinfo;
        Intent starter = new Intent(context, CustomerOrder.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("订单");
        addFragment(new SubFragment());
    }


    public static class SubFragment extends FastListFragment {

        @Override
        public int getItemLayoutRes() {
            return R.layout.manager_order_item;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {

            final SalesUserOrder_listInfo.Data item = (SalesUserOrder_listInfo.Data) getItem(position);

            fastText(convertView, R.id.tv_order_id, "订单号：" + item.getOrder_id());
            fastText(convertView, R.id.tv_date, item.getCreate_time());
            fastText(convertView, R.id.tv_pirce, "金额：￥" + item.getOrder_amount() + "    共" + item.getGood_sum() + "件商品");
            fastText(convertView, R.id.tv_status, item.getOrder_status());
            LinearLayout mLlGoods = findViewById(convertView, R.id.ll_goods);

            List<SalesUserOrder_listInfo.Data.Item> items = item.getItem();
            mLlGoods.removeAllViews();
            for (SalesUserOrder_listInfo.Data.Item subItem : items) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.order_check_good_item, null);

                ImageView mIvLogo = view.findViewById(R.id.iv_logo);
                TextView mTvName = view.findViewById(R.id.tv_name);
                TextView mTvDes = view.findViewById(R.id.tv_des);
                TextView mTvPrice = view.findViewById(R.id.tv_price);
                TextView mTvCount = view.findViewById(R.id.tv_count);

                ImgLoader.display(mIvLogo, subItem.getItem_img());
                setText(mTvName, subItem.getCommodity_name());
                setText(mTvDes, subItem.getSpec_info());
                setText(mTvPrice, "￥" + subItem.getPrice() + subItem.getUnit());
                setText(mTvCount, "x" + subItem.getNumber());

                mLlGoods.addView(view);
            }
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            M.salesUserOrder_list(mmember_id, mPage, mdateinfo, new CCallBack<SalesUserOrder_listInfo>() {
                @Override
                public void onSuccess(SalesUserOrder_listInfo o) throws Exception {
                    List<SalesUserOrder_listInfo.Data> data = o.getData();
                    if (mPage.equals("0")) {
                        mAdapter.setData(data);
                    } else {
                        mAdapter.addAll(data);
                    }
                    if (mAdapter.getDataSize() > 0) {
                        mPage = ((SalesUserOrder_listInfo.Data) getLast()).getId();
                    }
                    onLoadFinishHasMore(data.size() >= Api.PAGE_SIZE);
                }

                @Override
                public void onFailure(int code, String msg) {
                    onLoadFinishNoMore();
                    toast(msg);
                }
            });
        }

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            //SalesUserOrder_listInfo.Data item = (SalesUserOrder_listInfo.Data) getItem(position);
        }
    }
}